﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Chiu trach nhiem: HOANG NGOC PHUONG 
/// Cac ham thao tac doi doi tuong,dac bien la DropDownList
/// Khong chiu trach nhieu voi cac thanh vien su dung lai gay loi :D
/// </summary>
public class obj
{
	public obj()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static void fnCreateObjDdlSex(DropDownList ddl, string sSlected)
    {
        ddl.Items.Add(new ListItem("Giới tính", ""));
        ddl.Items.Add(new ListItem("Nam", "0"));
        ddl.Items.Add(new ListItem("Nữ", "1"));
        try
        {
            ddl.SelectedValue = sSlected;
        }
        catch
        {
        }
    }
    public static void fnCreateObjDdlCats(DropDownList ddl, string sSelected, string lang)
    {

        string strSQL = "SELECT id, catname From Cats  where lang = " + comm.to_sql(lang,"text") + " ORDER By Pos ";
        //string strSQL = "SELECT id, catname From Cats  ORDER By Pos ";
        //return strSQL;
        DataTable dt = dal.SelectTable(strSQL);
        ddl.Items.Add(new ListItem(sSelected, ""));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ddl.Items.Add(new ListItem(dt.Rows[i]["catname"].ToString(), dt.Rows[i]["id"].ToString()));

        }
        try
        {
            ddl.SelectedValue = sSelected;
        }
        catch
        {

        }
    }

    public static void fnCreateObjDdlSHCats(DropDownList ddl, string sSelected, string lang)
    {

        string strSQL = "SELECT id, catname From ShCats  where lang = " + comm.to_sql(lang, "text") + " ORDER By Pos ";
        //return strSQL;
        DataTable dt = dal.SelectTable(strSQL);
        ddl.Items.Add(new ListItem(sSelected, ""));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ddl.Items.Add(new ListItem(dt.Rows[i]["catname"].ToString(), dt.Rows[i]["id"].ToString()));

        }
        try
        {
            ddl.SelectedValue = sSelected;
        }
        catch
        {

        }
    }


    public static void fnCreateObjDdlCatsParent(DropDownList ddl, string sSelected, string lang)
    {

        string strSQL = "SELECT id, catname From Cats  where lang = " + comm.to_sql(lang, "text") + " and parentid = 0 ORDER By Pos ";
        //string strSQL = "SELECT id, catname From Cats  where parentid = 0 ORDER By Pos ";
        //return strSQL;
        DataTable dt = dal.SelectTable(strSQL);
        ddl.Items.Add(new ListItem(sSelected, "0"));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ddl.Items.Add(new ListItem(dt.Rows[i]["catname"].ToString(), dt.Rows[i]["id"].ToString()));

        }
        try
        {
            ddl.SelectedValue = sSelected;
        }
        catch
        {

        }
    }

    public static void fnCreateObjDdlParentIntro(DropDownList ddl, string sSelected, string lang)
    {

        string strSQL = "SELECT id, title From Introduce  where parentid = 0 and lang = " + comm.to_sql(lang, "text") + " ORDER By Pos ";
        //string strSQL = "SELECT id, catname From Cats  where parentid = 0 ORDER By Pos ";
        //return strSQL;
        DataTable dt = dal.SelectTable(strSQL);
        ddl.Items.Add(new ListItem(sSelected, "0"));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ddl.Items.Add(new ListItem(dt.Rows[i]["Title"].ToString(), dt.Rows[i]["id"].ToString()));

        }
        try
        {
            ddl.SelectedValue = sSelected;
        }
        catch
        {

        }
    }

    public static bool GetParent(string ID, string lang)
    {
        //string strSQL = "SELECT id, catname From Cats  where lang = " + comm.to_sql(lang, "text") + " and parentid = 0 ORDER By Pos ";
        string strSQL = "SELECT Parentid From Cats  where id = " + comm.to_sql(ID,"number") + " and lang = " + comm.to_sql(lang, "text");
        //return strSQL;
        DataTable dt = dal.SelectTable(strSQL);
        if (dt.Rows[0]["parentID"].ToString() != "0")
            return true;
        return false;
    }

    public static void fnCreateObjDdlProjectCats(DropDownList ddl, string sSelected, string lang)
    {

        string strSQL = "SELECT id, catname From ProjectCats  where lang = " + comm.to_sql(lang, "text") + " ORDER By Pos ";
        //return strSQL;
        DataTable dt = dal.SelectTable(strSQL);
        ddl.Items.Add(new ListItem(sSelected, ""));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ddl.Items.Add(new ListItem(dt.Rows[i]["catname"].ToString(), dt.Rows[i]["id"].ToString()));

        }
        try
        {
            ddl.SelectedValue = sSelected;
        }
        catch
        {

        }
    }


    public static void fnCreateObjDdlAdv(DropDownList ddl, string sSelected, string nametable, string Field)
    {
        string strSQL = "SELECT id," + Field + " From " + nametable + " ORDER By Pos ";
        //return strSQL;
        DataTable dt = dal.SelectTable(strSQL);

        ddl.Items.Add(new ListItem(sSelected, ""));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ddl.Items.Add(new ListItem(dt.Rows[i][Field].ToString().Trim(), dt.Rows[i]["id"].ToString().Trim()));

        }
        try
        {
            ddl.SelectedValue = sSelected;
        }
        catch
        {

        }
    }
    public static void fnCreateObjDdlAdv(DropDownList ddl, string sSelected, string nametable, string Field, string lang)
    {
        string strSQL = "SELECT id," + Field + " From " + nametable + " Where lang = " + comm.to_sql(lang,"text") + " ORDER By Pos ";
        //return strSQL;
        DataTable dt = dal.SelectTable(strSQL);

        ddl.Items.Add(new ListItem(sSelected, ""));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ddl.Items.Add(new ListItem(dt.Rows[i][Field].ToString().Trim(), dt.Rows[i]["id"].ToString().Trim()));

        }
        try
        {
            ddl.SelectedValue = sSelected;
        }
        catch
        {

        }
    }


    public static void fnCreateObjDdllang(DropDownList ddl)
    {
        
        try
        {
            ddl.Items.Add(new ListItem("Việt Nam", "vn"));
            ddl.Items.Add(new ListItem("English", "en"));
        }
        catch
        {
        }
    }

    public static void fnCreateObjDdl(DropDownList ddl, string sSelected, string nametable,string Field,string lang)
    {
        string strSQL = "SELECT id,";
        strSQL = strSQL + comm.to_sql(Field, "text");
        strSQL = strSQL + " From  Cats ";
        strSQL = strSQL + " Where ParentId = 0 and lang = ";
        strSQL = strSQL + comm.to_sql(lang, "text");
        strSQL = strSQL + " ORDER By Pos ";
        //return strSQL; 
        DataTable dt = dal.SelectTable(strSQL);

        ddl.Items.Add(new ListItem(sSelected, ""));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ddl.Items.Add(new ListItem(dt.Rows[i][Field].ToString().Trim(), dt.Rows[i]["id"].ToString().Trim()));

        }
        try
        {
            ddl.SelectedValue = sSelected;
        }
        catch
        {

        }
    }

    public static void fnCreateObjDdlYear(DropDownList ddl, string sSlected)
    {
        ddl.Items.Add(new ListItem("Năm", ""));
        for (int i = 1950; i <= 3000; i++)
        {
            string sTemp = "";
            sTemp = i.ToString();
            ddl.Items.Add(new ListItem(sTemp, sTemp));
        }
        try
        {
            ddl.SelectedValue = sSlected;
        }
        catch
        {
        }
    }
    public static void fnCreateObjDdlDay(DropDownList ddl, string sSlected)
    {
        ddl.Items.Add(new ListItem("Ngày", ""));
        fnCreateObjDdlDate(ddl, 1, 31, sSlected);
    }
    public static void fnCreateObjDdlMonth(DropDownList ddl, string sSlected)
    {
        ddl.Items.Add(new ListItem("Tháng", ""));
        fnCreateObjDdlDate(ddl, 1, 12, sSlected);
    }
    public static void fnCreateObjDdlHour(DropDownList ddl, string sSlected)
    {
        ddl.Items.Add(new ListItem("Giờ", ""));
        fnCreateObjDdlDate(ddl, 0, 23, sSlected);
    }
    public static void fnCreateObjDdlMinute(DropDownList ddl, string sSlected)
    {
        ddl.Items.Add(new ListItem("Phút", ""));
        fnCreateObjDdlDate(ddl,0,59,sSlected);
    }
    public static string fnFullDate(string sDate)
    {
        if (sDate.Length < 2) return "0" + sDate;
        return sDate;
    }
    public static void fnCreateObjDdlDate(DropDownList ddl, int iF, int iL, string sSlected)
    {
        for (int i = iF; i <= iL; i++)
        {
            string sTemp = "";
            sTemp = fnFullDate(i.ToString());
            ddl.Items.Add(new ListItem(sTemp, sTemp));
        }
        try {
            ddl.SelectedValue = sSlected;
        }
        catch { 
        }
    }
    public static string fnGetDay(string sDate)
    {
        if (sDate.Length == 10) return sDate.Substring(8,2);
        return sDate;
    }
    public static string fnGetMonth(string sDate)
    {
        if (sDate.Length == 10) return sDate.Substring(5, 2);
        return sDate;
    }

    public static string fnGetYear(string sDate)
    {
        if (sDate.Length == 10) return sDate.Substring(0,4);
        return sDate;
    }

    public static void fnBindList(DropDownList ddl, DataTable dt, string sSelected)
    {
        ddl.DataTextField = "Name";
        ddl.DataValueField = "id";
        ddl.DataSource = dt;
        ddl.DataBind();
        try
        {
            ddl.SelectedValue = sSelected;
        }
        catch
        {

        }
    }
    public static void fnBindListCountry(DropDownList ddl, string sSelected)
    {
        string strsql;
        strsql = "(select '' as Id,'...Select...' as Name  from countries)union(select CountryId,CountryName from countries)";
        DataTable dt;
        dt = dal.SelectTable(strsql);
        fnBindList(ddl, dt, sSelected);
    }
    public static void fnBindListCity(DropDownList ddl, string sCountryId, string sSelected)
    {
        string strsql;
        strsql = "(select '' as Id,'...Select...' as Name  from countries)union(select CityId,CityName from Cities where CountryID=" + sCountryId + ")";
        DataTable dt;
        dt = dal.SelectTable(strsql);
        fnBindList(ddl, dt, sSelected);
    }
    public static void fnBindListPhotoCats(DropDownList ddl, string sSelected)
    {
        string strsql;
        strsql = "(select '0' as Id,'...Select...' as Name  from countries)union(select ID, CatName from PhotoCats)";
        DataTable dt;
        dt = dal.SelectTable(strsql);
        fnBindList(ddl, dt, sSelected);
    }


    public static string GetparentbyId(string Parentid, string nametable, string Field)
    {
        string strSQL = "SELECT " + Field + " From " + nametable + " Where Id = " + Parentid;
        DataTable dt = dal.SelectTable(strSQL);
        return dt.Rows[0][Field].ToString();
    }

    public static string GetProjectId(string id)
    {
        string strSQL = "SELECT ProjectId From Contents Where id = " + comm.to_sql(id, "text");
        DataTable dt = dal.SelectTable(strSQL);
        string sProjectId = "";
        if (dt.Rows.Count > 0 && dt.Rows[0]["ProjectId"].ToString() != "0")
        {
            sProjectId = dt.Rows[0]["ProjectId"].ToString();
        }

        return sProjectId;
    }
    public static string GetCatId(string id)
    {
        string strSQL = "SELECT CatId From Contents Where id = " + comm.to_sql(id, "text");
        DataTable dt = dal.SelectTable(strSQL);
        string sCatId = "";
        if (dt.Rows.Count > 0 && dt.Rows[0]["CatId"].ToString() !="0" )
        {
            sCatId = dt.Rows[0]["CatId"].ToString();
        }
        return sCatId;
    }

    public static string GetValueCatId(string Catid)
    {
        string titlex = "";
        if (Catid == "") Catid = "0";

        if (Catid == "0" || Catid == "1")
            titlex = lang.getVariableValue("mnNews");
        if (Catid == "4")
            titlex = lang.getVariableValue("mnShareholder");
        if (Catid == "6")
            titlex = lang.getVariableValue("mnCareerOpportunities");

        return titlex;
    }

    public static string GetShortDescIntro(string str)
    {
        if (str == "")
        {
            str = " ";
        }
        else
        {
            str = getStrExtract(str,1500);
        }
        return str;
    }

    public static string GetShortDesc(string str,int nChar)
    {
        if (str == "")
        {
            str = " ";
        }
        else
        {
            str = getStrExtract(str, nChar);
        }
        return str;
    }


    public static string GetTitlePhoto(string sPhoto)
    {
        if (sPhoto == "")
        {
            sPhoto = " ";
        }
        else
        {
            sPhoto = getStrExtract(sPhoto, 15);
        }
        return sPhoto;
    }

    public static string getStrExtract(string sStr, int len)
    {
        string oStr;
        if (len <= 3 || sStr.Length <= len)
        {
            oStr = sStr;
        }
        else
        {
            oStr = sStr.Substring(0, len - 3) + "...";
        }
        return oStr;
    }

    public static string FormatStr(string str)
    {
        if (str == "")
        {
            str = " ";
        }
        else
        {
            str = getStrExtract(str, 200);
        }
        return str;
    }


    public static string GetFieldByFieldNumber(string TableName, string GetFieldName, string FieldFilter, string FieldValue)
    {
        string stsql;
        stsql = "SELECT " + GetFieldName + " as fName FROM " + TableName + " where " + FieldFilter + "=" + comm.to_sql(FieldValue, "number") + " and lang = " + comm.to_sql(lang.getLang(), "text");
        DataTable dt;
        dt = dal.SelectTable(stsql);
        if (dt.Rows.Count > 0 )
        {
            return dt.Rows[0]["fName"].ToString();
        }

        System.Web.HttpContext.Current.Response.Redirect("/");
        return "";
    }


    public static void fnCreateObjDdlStatus(DropDownList ddl, string sSlected)
    {
        ddl.Items.Add(new ListItem("Tất cả", ""));
        ddl.Items.Add(new ListItem("Chưa trả lời", "0"));
        ddl.Items.Add(new ListItem("Đã trả lời", "1"));
        try
        {
            ddl.SelectedValue = sSlected;
        }
        catch
        {
        }
    }
    /// <summary>
    /// phan load file type
    /// </summary>
    /// <returns></returns>
    public static void fnBindListFileTypes(DropDownList ddl, string sSelected)
    {
        fnBindListFileTypes(ddl, sSelected, "--Lựa chọn--");
    }
    public static void fnBindListFileTypes(DropDownList ddl, string sSelected, string sCaption)
    {
        string sqlStr = "select ID as ID, FileType as Name from FileTypes order by FileType";
        DataTable dt = dal.SelectTable(sqlStr);

        ddl.Items.Clear();
        ListItem li = new ListItem(sCaption, "");
        ddl.Items.Add(li);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            li = new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Id"].ToString());
            ddl.Items.Add(li);
        }
        try
        {
            ddl.SelectedValue = sSelected;
        }
        catch
        {

        }
    }
}
